﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ICeFramework.DataAccess.Enums;
using System.Runtime.Serialization;
using ICeFramework.Common.DataLayer.Enums;

namespace ICeFramework.DataAccess.SettingsStorage
{
    [Serializable]
    public class SqlDataStorageEntity
    {
        private string _connectionString;
        #region public string ConnectionString
        /// <summary>
        /// Gets the ConnectionString of the SqlDataStorageEntity
        /// </summary>
        /// <value></value>
        public string ConnectionString
        {
            get { return _connectionString; }
        }
        #endregion

        private ServerType _serverType;
        #region public ServerType ServerType
        /// <summary>
        /// Get/Sets the ServerType of the SqlDataStorageEntity
        /// </summary>
        /// <value></value>
        public ServerType ServerType
        {
            get { return _serverType; }
            set
            {
                _serverType = value;
                SetConnectionString();
            }
        }
        #endregion

        private string _server;
        #region public string Server
        /// <summary>
        /// Get/Sets the Server of the SqlDataStorageEntity
        /// </summary>
        /// <value></value>
        public string Server
        {
            get { return _server; }
            set
            {
                _server = value;
                SetConnectionString();
            }
        }
        #endregion

        private string _port = "3306";
        #region public string Port
        /// <summary>
        /// Get/Sets the Port of the SqlDataStorageEntity
        /// </summary>
        /// <value></value>
        public string Port
        {
            get { return _port; }
            set
            {
                _port = value;
                SetConnectionString();
            }
        }
        #endregion

        private string _database;
        #region public string Database
        /// <summary>
        /// Get/Sets the Database of the SqlDataStorageEntity
        /// </summary>
        /// <value></value>
        public string Database
        {
            get { return _database; }
            set
            {
                _database = value;
                SetConnectionString();
            }
        }
        #endregion

        private string _userName;
        #region public string UserName
        /// <summary>
        /// Get/Sets the UserName of the SqlDataStorageEntity
        /// </summary>
        /// <value></value>
        public string UserName
        {
            get { return _userName; }
            set
            {
                _userName = value;
                SetConnectionString();
            }
        }
        #endregion

        private string _password;
        #region public string Password
        /// <summary>
        /// Get/Sets the Password of the SqlDataStorageEntity
        /// </summary>
        /// <value></value>
        public string Password
        {
            get { return _password; }
            set
            {
                _password = value;
                SetConnectionString();
            }
        }
        #endregion

        private bool _trustedConnection;
        #region public bool TrustedConnection
        /// <summary>
        /// Get/Sets the TrustedConnection of the SqlDataStorageEntity
        /// </summary>
        /// <value></value>
        public bool TrustedConnection
        {
            get { return _trustedConnection; }
            set
            {
                _trustedConnection = value;
                SetConnectionString();
            }
        }
        #endregion

        #region public SqlDataStorageEntity()
        /// <summary>
        /// Initializes a new instance of the <b>SqlDataStorageEntity</b> class.
        /// </summary>
        public SqlDataStorageEntity()
        {

        }
        #endregion

        #region private void SetConnectionString()
        /// <summary>
        /// 
        /// </summary>
        private void SetConnectionString()
        {
            string connStr = string.Empty;
            if (_serverType == ServerType.MsSql)
            {
                connStr += string.Format("Server={0};", _server);
                if (!string.IsNullOrEmpty(_database))
                {
                    connStr += string.Format("Database={0};", _database);
                }
                if (_trustedConnection)
                {
                    connStr += "Trusted_Connection=True;";
                }
                else
                {
                    connStr += string.Format("User ID={0};", _userName);
                    connStr += string.Format("Password={0};", _password);
                }
            }
            else
            {
                connStr += string.Format("Server={0};", _server);
                if (!string.IsNullOrEmpty(_database))
                {
                    connStr += string.Format("Database={0};", _database);
                }
                connStr += string.Format("User ID={0};", _userName);
                connStr += string.Format("Password={0};", _password);
                if (string.IsNullOrEmpty(_port))
                {
                    connStr += "Port=3306";
                }
                else
                {
                    connStr += string.Format("Port={0}", _port);
                }
            }
            _connectionString = connStr;
        }
        #endregion
    }
}
